package org.example.traffic.organizationadmin;

import org.example.traffic.organization.Organization;
import org.example.traffic.organizationadmin.OrganizationAdmin;
import org.example.traffic.organizationadmin.OrganizationAdminRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class OrganizationAdminService {
    private final OrganizationAdminRepository organizationAdminRepository;

    public OrganizationAdminService(OrganizationAdminRepository organizationAdminRepository) {
        this.organizationAdminRepository = organizationAdminRepository;
    }

    // 注册方法，保存组织管理员信息到数据库
    public OrganizationAdmin register(OrganizationAdmin organizationAdmin) throws IllegalArgumentException {
        OrganizationAdmin existingAdmin = organizationAdminRepository.findByUsername(organizationAdmin.getUsername());
        if (existingAdmin!= null) {
            throw new IllegalArgumentException("用户名已存在");
        }
        return organizationAdminRepository.save(organizationAdmin);
    }

    // 登录方法，根据用户名和密码验证并返回对应的组织管理员信息（如果存在）
    public Optional<OrganizationAdmin> login(String username, String password) {
        OrganizationAdmin organizationAdmin = organizationAdminRepository.findByUsername(username);
        if (organizationAdmin!= null && organizationAdmin.getPassword().equals(password)) {
            return Optional.of(organizationAdmin);
        }
        return Optional.empty();
    }

    // 保存（新增）组织管理员信息
    public OrganizationAdmin save(OrganizationAdmin organizationAdmin) {
        OrganizationAdmin existingAdmin = organizationAdminRepository.findByUsername(organizationAdmin.getUsername());
        if (existingAdmin!= null && !existingAdmin.getId().equals(organizationAdmin.getId())) {
            // 如果存在同名的其他管理员且不是当前要更新的这个管理员（通过ID判断），则抛出异常
            throw new IllegalArgumentException("用户名已存在");
        }
        return organizationAdminRepository.save(organizationAdmin);
    }
    // 更新组织管理员信息
    public OrganizationAdmin update(OrganizationAdmin organizationAdmin) {
        OrganizationAdmin existingAdmin = organizationAdminRepository.findByUsername(organizationAdmin.getUsername());
        if (existingAdmin!= null && !existingAdmin.getId().equals(organizationAdmin.getId())) {
            // 如果存在同名的其他管理员且不是当前要更新的这个管理员（通过ID判断），则抛出异常
            throw new IllegalArgumentException("用户名已存在");
        }
        return organizationAdminRepository.save(organizationAdmin);
    }
    // 根据ID查找组织管理员信息（返回Optional类型，可能不存在）
    public Optional<OrganizationAdmin> findById(Integer id) {
        return organizationAdminRepository.findById(id);
    }

    // 查找所有组织管理员信息
    public List<OrganizationAdmin> findAll() {
        return organizationAdminRepository.findAll();
    }

    // 根据ID删除组织管理员信息
    public void deleteById(Integer id) {
        organizationAdminRepository.deleteById(id);
    }

    // 分页查询视频
    public Page<OrganizationAdmin> getAdmins(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        return organizationAdminRepository.findAll(pageable);
    }


    // 根据用户名模糊查询
    public Page<OrganizationAdmin> findByUsernameContaining(String username, int page, int size) {
        return organizationAdminRepository.findByUsernameContainingIgnoreCase(username, PageRequest.of(page, size));
    }

}
